import rxAjax from '@/assets/ajax.js';


//表单接口
export const FormApi = {};


/**
 * 给指定的URL提交JSON对象。
 * @param  url
 * @param  params json对象
 */
FormApi.postJson= function(url,params,config){
    return  rxAjax.postJson(url,params,config)
}

/**
 * 给指定的URL PUT JSON对象。
 * @param  url
 * @param  params json对象
 */
FormApi.putJson= function(url,params,config){
    return  rxAjax.putJson(url,params,config)
}

/**
 * 给指定的URL提交JSON对象。
 * @param  url
 * @param  params json对象
 */
FormApi.get= function(url,params,config){
    return  rxAjax.get(url,params,config);
}
/**
 * 提交数据。
 * params 两种方式 ： 对象 {name:"RAY"} 传键值对 name=ray
 * @param url
 * @param params
 */
FormApi.postForm= function(url,params,config){
    return  rxAjax.postForm(url,params,config);
}


/**
 * 根据别名查询数据字典数据
 */
FormApi.getDicByAlias=function(dicKey,appId){
    let url="/api-system/system/core/sysDic/listByKey?key="+dicKey+"&appId="+appId;
    return rxAjax.get(url);
}

/**
 * 调用脚本
 */
FormApi.invoke= function(alias,params){
    var url="/api-form/form/core/formInvokeScript/invoke/"+alias;
    return rxAjax.postJson(url,params);
}

/**
 * 自定义SQL
 */
FormApi.queryForJson=function(sqlKey,params){
    let url= "/api-form/form/core/formCustomQuery/queryForJson_"+sqlKey;
    return rxAjax.postForm(url,params);
}

/**
 * 第三方接口
 */
FormApi.executeInterfaceApi=function(apiKey,params){
    let url = "/api-system/system/core/sysInterfaceApi/executeApi?apiKey="+apiKey;
    return rxAjax.postJson(url,params);
}
/**
 *数据权限
 */
FormApi.loadByGrant=function(grantkey){
    let url = "/api-form/form/core/formEntityDataSetting/queryData_"+grantkey;
    return rxAjax.get(url);
}

//根据key获取当前自定义查询对象
FormApi.getSqlByKey=function (key){
    let url= 'api-form/form/core/formCustomQuery/getByKey?key=' + key;
    return rxAjax.get(url);
}

//根据key获取当前第三方接口对象
FormApi.getInterfaceApiByApiKey=function (apiKey){
    let url= "/api-system/system/core/sysInterfaceApi/getByApiKey?apiKey="+apiKey;
    return rxAjax.get(url);
}
/**
 * 保存base64数据到文件系统。
 * @param data
 * @returns {*}
 */
FormApi.saveBase64=function (data,color){
    let url ="/api-system/system/core/sysFile/saveBase64";
    let params={data:data,color:color};
    return rxAjax.postJson(url,params);
}

FormApi.getSignatureList=function () {
    let url="/api-system/system/core/sysSignature/getSignatureList";
    return rxAjax.get(url);
}

FormApi.delSignature=function (ids) {
    let url=`/api-system/system/core/sysSignature/del?ids=${ids}`;
    return rxAjax.postJson(url);
}
//保存签名
FormApi.saveSignature=function (data,color) {
    let params={data:data,color:color};
    let url="/api-system/system/core/sysSignature/saveSignature";
    return rxAjax.postJson(url,params);
}
FormApi.uploadSignature=function(params) {
    var url="/api-system/system/core/sysSignature/uploadSignature";
    return rxAjax.upload(url,params).then (res => {
        return res.data
    });
}


//获取签名地址
FormApi.getSignatureAddress=function () {
    let url="/api-system/system/core/sysSignature/getSignatureAddress";
    return rxAjax.get(url);
}

FormApi.getSignature=function (timestamp,timeout) {
    let url="/api-system/system/core/sysSignature/getSignature";
    let params={timestamp:timestamp};
    let config={timeout:timeout}
    return rxAjax.postTimeout(url,params,config);
}

/*地址控件*/
FormApi.queryAddress=function(addressKey,config) {
    let params={config:JSON.stringify(config),addressKey:addressKey};
    let url= "/api-form/form/share/formAddress/queryAddress";
    return rxAjax.postForm(url,params);
}

//根据key与父id查询数据字典
FormApi.loadDicByPidAndDicId=function(dicKey,parentId) {
    let url= "/api-system/system/core/sysDic/getByPidAndDicId?dicKey="+dicKey+"&parentId="+parentId;
    return rxAjax.get(url);
}

//上传文件
FormApi.upload=function(formData,callback) {
    let url= '/api-system/system/core/sysFile/upload';
    return rxAjax.upload(url,formData,callback);
}

/**
 * 获取当前用户的应用管理授权
 */
FormApi.getManagerOfCurUser =function () {
    var url=  '/api-system/system/core/sysAppManager/getManagerOfCurUser';
    return rxAjax.get(url);
}

FormApi.getPinyin =function(chinese) {
    var url= "/api-form/form/share/pinyin/getPinyin";
    if(!chinese){
        return ;
    }
    var params={chinese:chinese,lower:true};
    return rxAjax.postForm(url,params);
}

/**
 * 根据表单id获取数据。
 */
FormApi.getFormPcById =function(pkId) {
    var url= '/api-form/form/core/formPc/getById?pkId=' + pkId;
    return rxAjax.get(url);
}

FormApi.saveFormPc =function(parameter,isDeploy) {
    var url= '/api-form/form/core/formPc/saveForm';
    if(isDeploy){
        url+="?isDeploy="+isDeploy;
    }
    return rxAjax.postJson(url,parameter);
}

/**
 * 表单暂存处理.
 * @param {Object} parameter 表单参数
 * @returns {*}
 */
FormApi.saveTemporaryForm =function(parameter) {
    var url= '/api-form/form/core/formPc/temporaryForm';
    return rxAjax.postJson(url,parameter);
}



FormApi.validFormPc =function(parameter) {
    var url= '/api-form/form/core/formPc/validForm';
    return rxAjax.postJson(url,parameter);
}

FormApi.genFormPc =function(parameter) {
    var url= '/api-form/form/core/formPc/genForm';
    return rxAjax.postJson(url,parameter);
}

//发布表单
FormApi.deployFormPc =function(formId,genSolution) {
    genSolution=genSolution||false;
    var url= '/api-form/form/core/formPc/deploy';
    var params={formId:formId,genSolution:genSolution};
    return rxAjax.postForm(url,params);
}
//获取常量
FormApi.getConstVars=function() {
    var url= "/api-user/user/org/public/getConstVars";
    return rxAjax.get(url);
}

/**
 * 获取数据权限列表
 * @param (String) boListKey 列表key
 * @returns {*}
 */
FormApi.getPmtListByKey=function(boListKey) {
    let url = "/api-form/form/core/formBoPermission/getListByKey?boListKey=" + boListKey;
    return rxAjax.get(url);
}

/**
 * 获取表单的字段
 * @param (String) formAlias 表单方案别名
 * @returns {*}
 */
FormApi.getListFields=function (formAlias){
    let url= '/api-form/form/core/formSolution/getListFields?formAlias=' + formAlias;
    return rxAjax.get(url);
}

FormApi.getSolutionByAlias=function(alias,id,params,action) {
    let url= "/api-form/form/core/formSolution/" + alias + '/getByAlias'  ;
    let args=[];
    if(id){
        args.push("pk=" + id);
    }
    if(action){
        args.push("action=" + action);
    }
    if(args.length>0){
        url+="?" + args.join("&");
    }
    return rxAjax.postJson(url,params);
}

/**
 * 获取列表
 * @param conf
 * @returns {*}
 */
FormApi.getBoList=function(conf){
    let url= '/api-form/form/core/formBoList/' + conf.key +'/dialog';
    if(conf.params){
        url+="?" + conf.params;
    }
    if(conf.pmtAlias){
        if(url.indexOf("?")==-1){
            url+="?pmtAlias=" +conf.pmtAlias;
        } else{
            url+="&pmtAlias=" +conf.pmtAlias;
        }
    }
    if(conf.single){
        if(url.indexOf("?")==-1){
            url+="?single=" +conf.single;
        } else{
            url+="&single=" +conf.single;
        }
    }
    return rxAjax.get(url);
}

//根据instId获取审批历史
FormApi.getCheckHistorys=function (instId) {
    let url="/api-bpm/bpm/core/bpmCheckHistory/getCheckHistorys?instId="+ instId;
    return rxAjax.get(url);
}

//获取审批意见模板
FormApi.getOpinionTemplate=function (){
    var url ="/api-form/form/core/formPc/getOpinionTemplate";
    return rxAjax.get(url);
}

//获取审批类型
FormApi.getCheckTypes=function (){
    let url ="/api-system/system/core/sysProperties/getCheckTypes";
    return rxAjax.get(url);
}


//获取组织维度
FormApi.getDimList=function(){
    let url= "/api-user/user/org/osDimension/getDimList";
    return rxAjax.get(url);
}

//获取组织维度等级
FormApi.getRankLevelByDimId=function(dimId){
    var url= "/api-user/user/org/osRankType/getByDimId?dimId="+dimId;
    return rxAjax.get(url);
}

//根据类型获取印章列表
FormApi.getStampList=function (type){
    var url ="/api-form/form/core/formStamp/getStamps";
    let params;
    if(type){
        params={"type":type};
    }
    return rxAjax.postForm(url,params);
}

/**
 * 根据KEY查找城市。
 * @param key
 * @returns {*}
 */
FormApi.getCityByKey =function (key) {
    var url=  '/api-form/form/core/formCity/getByKey';
    let params={key:key};
    return rxAjax.postForm(url,params);
}

/**
 * 返回热门城市
 * @param categoryId
 * @returns {*}
 */
FormApi.getPopularCityByCategory =function (categoryId) {
    var url=  '/api-form/form/core/formPopularCity/getByCategory';
    let params={categoryId:categoryId};
    return rxAjax.get(url,params);
}

/**
 * 根据分类和编码获取城市
 * @param {String}  categoryId    分类
 * @param {String}  code          编码
 * @returns {*}
 */
FormApi.getByCategoryAndCode =function (categoryId,code) {
    var url=  '/api-form/form/core/formCity/getByCategoryAndCode';
    let params={
        pageNo:1,
        pageSize:5,
        params:{
            categoryId: categoryId,
            code: code
        }
    }
    return rxAjax.postJson(url,params);
}

/**
 * 通过jdbc元数据的api查找列名
 * @param parameter
 * {
 *     query:"表名",
 *     queryType:"查询类型",
 *     ds:"数据源",
 * }
 * @returns {*}
 */
FormApi.findColumnList= function(parameter){
    let url = '/api-form/form/core/sysDb/findColumnList' ;
    return rxAjax.postJson(url,parameter)
}

/**
 * 获取业务模型列表
 * @param parameter
 * @returns {*}
 */
FormApi.getTemplatesByBoDef=function(parameter){
    let url= '/api-form/form/core/formPc/getTemplatesByBoDef';
    return rxAjax.postForm(url,parameter);
}

/**
 * 根据BO生成表单
 * @param parameter
 * @returns {*}
 */
FormApi.genDragConfig=function(parameter){
    let url=  '/api-form/form/core/formPc/genDragConfig';
    return rxAjax.postForm(url,parameter);
}

//导入Excel模板
FormApi.downloadTemplate=function(fieldsConfig,comment,accessToken) {
    let params={fieldsConfig:JSON.stringify(fieldsConfig),comment:comment};
    let config={responseType:'arraybuffer'}
    return rxAjax.download("/api-form/form/core/formPc/downloadTemplate?accessToken="+accessToken,params,config);
}

//导入Excel
FormApi.uploadExcel=function(formData,beginRow,sheetNo,callback) {
    return rxAjax.upload("/api-form/form/core/formPc/uploadExcel?beginRow="+beginRow+"&sheetNo="+sheetNo,formData,callback);
}

//导出Excel
FormApi.exportExcel=function(params) {
    var config={responseType:'arraybuffer'}
    return rxAjax.download("/api-form/form/core/formPc/exportExcel",params,config);
}

//单值编辑按钮根据配置获取值
FormApi.getBtnEditSingleValue=function(parameter) {
    return rxAjax.postForm("/api-form/form/core/formPc/getBtnEditSingleValue",parameter);
}

//获取服务器路径
FormApi.getServerAddress=function(ctxPath) {
    var url="/api-form/form/core/formPc/getServerAddress?ctxPath="+ctxPath;
    return rxAjax.get(url);
}

FormApi.getBpmInstEncryptList=function(instIds,relType){
    var url="/api-bpm/bpm/core/bpmInst/getBpmInstEncryptList";
    return rxAjax.postJson(url,{instIds:instIds,relType:relType});
}

//选择人员查询输入拼音或者中文查询 人员数据
FormApi.getUserData = function (parameter){
    let url = '/api-user/user/org/osUser/getUser';
    return rxAjax.postJson(url,parameter);
}
//选择部门/组织查询输入拼音或者中文查询 部门/组织数据
FormApi.getGroupData = function (parameter){
    let url = '/api-user/user/org/osGroup/getGroup';
    return rxAjax.postJson(url,parameter);
}
FormApi.getBoDefConstruct=function(boDefId,includeCommon){
    var url= "/api-form/form/bo/formBoDef/getBoDefConstruct?boDefId="+boDefId+"&includeCommon="+includeCommon;
    return rxAjax.get(url);
}


/**
 * 根据pkId获取应用
 * @param pkId
 * @returns {*}
 */
FormApi.getAppById =function(pkId) {
    var url=  '/api-system/system/core/sysApp/getAppByPkId?pkId=' + (pkId || '0');
    return rxAjax.get(url);
}

/**
 *  验证唯一性
 * @param params
 * @returns {*}
 */
FormApi.checkUniqueValue =function(params) {
    var url= "/api-form/form/core/formPc/checkUniqueValue";
    return  rxAjax.postJson(url,params)
}

/**
 * 执行规则引擎
 * @param liteFlowId
 * @param params
 * @returns {*}
 */
FormApi.executeLiteFlow =function(liteFlowId, params) {
    let url= '/api-form/form/core/formLiteFlow/executeLiteFlow?liteFlowId='+liteFlowId;
    return rxAjax.postJson(url,params);
}
